Imports System.Windows.Forms

Imports TaxonNamesInterface
Imports NamesDataAccess

Public Class BaseImporter

    Public Delegate Sub StatusDelegate(ByVal percComplete As Integer, ByVal msg As String)

    Public StatusCallback As StatusDelegate
    Public Success As Boolean = False
    Public ImportObj As Import
    Public ImportType As ImportType
    Public Cancel As Boolean = False

    Protected PercentComplete As Integer '0 to 100

    Public ReadOnly Property Complete() As Boolean
        Get
            Return (PercentComplete = 100)
        End Get
    End Property

    Public Overridable Sub ImportFile()
    End Sub

    Protected Sub PostStatusMessage(ByVal percComplete As Integer, ByVal msg As String)
        If Not StatusCallback Is Nothing Then
            StatusCallback.Invoke(percComplete, msg)
        End If
    End Sub

    'TODO ...

    '''' <summary>
    '''' Save dataset of provider names or references
    '''' </summary>
    '''' <param name="providerRecords"></param>
    '''' <remarks>
    '''' Dataset contains tblProviderReference and tblProviderRIS or tblProviderName and tblProviderConcept tables. 
    '''' If a record already exists from a previous import then the record is updated with any new non-null values
    '''' </remarks>
    'Protected Sub SaveImportedDataset(ByVal providerRecords As DataSet)

    '    Try
    '        'insert provider import record
    '        NamesDataAccess.ImportData.InsertUpdateImport(ImportObj, SessionState.CurrentUser.Login)

    '        Dim recordCount As Integer = 0
    '        Dim recordPosition As Integer = 0

    '        Dim table As DataTable
    '        If ImportType.ObjectType = ImportType.ObjectTypeName Then
    '            If providerRecords.Tables.Contains("tblProviderName") Then recordCount += providerRecords.Tables("tblProviderName").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderConcept") Then recordCount += providerRecords.Tables("tblProviderConcept").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderConceptRelationship") Then recordCount += providerRecords.Tables("tblProviderConceptRelationship").Rows.Count
    '            table = providerRecords.Tables("tblProviderName")
    '        ElseIf ImportType.ObjectType = ImportType.ObjectTypeReference Then
    '            If providerRecords.Tables.Contains("tblProviderReference") Then recordCount += providerRecords.Tables("tblProviderReference").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderRIS") Then recordCount += providerRecords.Tables("tblProviderRIS").Rows.Count
    '            table = providerRecords.Tables("tblProviderReference")
    '        ElseIf ImportType.ObjectType = ImportType.ObjectTypeOtherData Then
    '            If providerRecords.Tables.Contains("tblProviderOtherData") Then recordCount += providerRecords.Tables("tblProviderOtherData").Rows.Count
    '            table = providerRecords.Tables("tblProviderOtherData")
    '        ElseIf ImportType.ObjectType = ImportType.ObjectTypeAll Then
    '            If providerRecords.Tables.Contains("tblProviderName") Then recordCount += providerRecords.Tables("tblProviderName").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderConcept") Then recordCount += providerRecords.Tables("tblProviderConcept").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderConceptRelationship") Then recordCount += providerRecords.Tables("tblProviderConceptRelationship").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderReference") Then recordCount += providerRecords.Tables("tblProviderReference").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderRIS") Then recordCount += providerRecords.Tables("tblProviderRIS").Rows.Count
    '            If providerRecords.Tables.Contains("tblProviderOtherData") Then recordCount += providerRecords.Tables("tblProviderOtherData").Rows.Count
    '            table = providerRecords.Tables("tblProviderName")
    '        End If

    '        While table IsNot Nothing
    '            For Each row As DataRow In table.Rows
    '                If Cancel Then
    '                    PercentComplete = 100
    '                    Success = False
    '                    PostStatusMessage(PercentComplete, "Cancelled")
    '                    Exit Sub
    '                Else
    '                    recordPosition += 1
    '                    PercentComplete = 10 + (recordPosition / recordCount * 90) '10 percent was done in loading
    '                    If PercentComplete > 99 Then PercentComplete = 99 ' cant be greater than 99 until DONE

    '                    Dim cnn As SqlClient.SqlConnection
    '                    Dim trans As SqlClient.SqlTransaction
    '                    Try
    '                        cnn = New SqlClient.SqlConnection(Configuration.ConfigurationManager.ConnectionStrings("compositae").ConnectionString)
    '                        cnn.Open()
    '                        trans = cnn.BeginTransaction()

    '                        'insert new record
    '                        InsertUpdateRecord(trans, row)

    '                        trans.Commit()

    '                        PostStatusMessage(PercentComplete, "")
    '                    Catch ex As Exception
    '                        If Not trans Is Nothing Then trans.Rollback()
    '                        Dim msg As String = "ERROR importing record at position : " + recordPosition.ToString() + " : " + ex.Message
    '                        PostStatusMessage(PercentComplete, msg)
    '                        NamesException.LogError(ex)
    '                    Finally
    '                        If Not cnn Is Nothing Then cnn.Close()
    '                    End Try
    '                End If
    '            Next

    '            If ImportType.ObjectType = ImportType.ObjectTypeName Then
    '                If table.TableName = "tblProviderName" Then
    '                    table = providerRecords.Tables("tblProviderConcept")
    '                ElseIf table.TableName = "tblProviderConcept" Then
    '                    table = providerRecords.Tables("tblProviderConceptRelationship")
    '                Else
    '                    table = Nothing
    '                End If
    '            ElseIf ImportType.ObjectType = ImportType.ObjectTypeReference Then
    '                If table.TableName = "tblProviderReference" Then
    '                    table = providerRecords.Tables("tblProviderRIS")
    '                Else
    '                    table = Nothing
    '                End If
    '            ElseIf ImportType.ObjectType = ImportType.ObjectTypeAll Then
    '                If table.TableName = "tblProviderName" Then
    '                    table = providerRecords.Tables("tblProviderConcept")
    '                ElseIf table.TableName = "tblProviderConcept" Then
    '                    table = providerRecords.Tables("tblProviderConceptRelationship")
    '                ElseIf table.TableName = "tblProviderConceptRelationship" Then
    '                    table = providerRecords.Tables("tblProviderReference")
    '                ElseIf table.TableName = "tblProviderReference" Then
    '                    table = providerRecords.Tables("tblProviderRIS")
    '                ElseIf table.TableName = "tblProviderRIS" Then
    '                    table = providerRecords.Tables("tblProviderOtherData")
    '                Else
    '                    table = Nothing
    '                End If
    '            Else
    '                table = Nothing
    '            End If
    '        End While

    '        'status message for success/failure + perc complete
    '        PercentComplete = 100
    '        Success = True
    '        PostStatusMessage(PercentComplete, "SUCCESS")
    '    Catch ex As Exception
    '        NamesException.LogError(ex)
    '        PercentComplete = 100
    '        Success = False
    '        PostStatusMessage(PercentComplete, "FAILED")
    '    End Try

    'End Sub

    'Private Sub InsertUpdateRecord(ByVal trans As SqlClient.SqlTransaction, ByVal row As DataRow)
    '    If row.Table.TableName = "tblProviderName" Then
    '        Dim pn As New ProviderName(row, -1)

    '        'get existing record?
    '        Dim updatePN As ProviderName
    '        If row.Table.Columns.Contains("PNNameId") Then updatePN = NameData.GetProviderName(Provider.IdAsInt, row("PNNameId").ToString)
    '        If updatePN IsNot Nothing Then
    '            updatePN.UpdateFieldsFromProviderName(pn)
    '            updatePN.PNLinkStatus = LinkStatus.Unmatched.ToString 'reset so it will be relinked
    '            updatePN.PNNameFk = Nothing
    '        Else
    '            updatePN = pn
    '            updatePN.PNLinkStatus = LinkStatus.Unmatched.ToString
    '        End If
    '        updatePN.PNProviderImportFk = ProvImport.IdAsInt

    '        'update name
    '        NamesDataAccess.NameData.InsertUpdateProviderNameRecord(trans, updatePN, SessionState.CurrentUser.Login)

    '        'set rank so integration order can be done in rank top down order
    '        updatePN.PNNameRankFk = NameData.LinkProviderNameRank(trans, updatePN.IdAsInt, SessionState.CurrentUser.Login)
    '    End If

    '    If row.Table.TableName = "tblProviderConcept" Then
    '        Dim pc As New ProviderConcept(row, -1)

    '        'get existing record?
    '        Dim updateC As ProviderConcept
    '        If row.Table.Columns.Contains("PCConceptId") Then updateC = ConceptData.GetProviderConcept(Provider.IdAsInt, row("PCConceptId").ToString)
    '        If updateC IsNot Nothing Then
    '            updateC.UpdateFieldsFromProviderConcept(pc)
    '            updateC.PCLinkStatus = LinkStatus.Unmatched.ToString 'reset so it will be relinked when integration is done
    '            updateC.PCConceptFk = -1
    '        Else
    '            updateC = pc
    '            updateC.PCLinkStatus = LinkStatus.Unmatched.ToString
    '        End If
    '        updateC.PCProviderImportFk = ProvImport.IdAsInt

    '        'update concept
    '        ConceptData.InsertUpdateProviderConcept(trans, updateC, SessionState.CurrentUser.Login)

    '    End If

    '    If row.Table.TableName = "tblProviderConceptRelationship" Then
    '        Dim pcr As New ProviderConceptRelationship(row, -1)

    '        'get existing record?
    '        Dim updateCR As ProviderConceptRelationship
    '        If row.Table.Columns.Contains("PCRConcept1Id") AndAlso row.Table.Columns.Contains("PCRConcept2Id") AndAlso row.Table.Columns.Contains("PCRRelationship") Then
    '            updateCR = ConceptData.GetProviderConceptRelationship(Provider.IdAsInt, row("PCRConcept1Id").ToString, row("PCRConcept2Id").ToString(), row("PCRRelationship").ToString)
    '        End If
    '        If updateCR IsNot Nothing Then
    '            updateCR.UpdateFieldsFromProviderConceptRelationship(pcr)
    '            updateCR.PCRLinkStatus = LinkStatus.Unmatched.ToString 'reset so it will be relinked when integration is done
    '            updateCR.PCRConceptRelationshipFk = Nothing
    '        Else
    '            updateCR = pcr
    '            updateCR.PCRLinkStatus = LinkStatus.Unmatched.ToString
    '        End If
    '        updateCR.PCRProviderImportFk = ProvImport.IdAsInt

    '        'update concept
    '        ConceptData.InsertUpdateProviderConceptRelationship(trans, updateCR, SessionState.CurrentUser.Login)

    '        'link relationship type for when we integrate names (need to know the parent concept linkage to integarte a name)
    '        updateCR.PCRRelationshipFk = NameData.LinkProviderConceptRelationship(trans, updateCR.IdAsInt, SessionState.CurrentUser.Login)
    '    End If

    '    If row.Table.TableName = "tblProviderReference" Then
    '        Dim r As New ProviderReference(row, -1)

    '        'get existing record?
    '        Dim updateRef As ProviderReference
    '        If row.Table.Columns.Contains("PRReferenceId") Then updateRef = ReferenceData.GetProviderReference(Provider.IdAsInt, row("PRReferenceId").ToString)
    '        If updateRef IsNot Nothing Then
    '            updateRef.UpdatedFieldsFromProviderReference(r)
    '            updateRef.PRLinkStatus = LinkStatus.Unmatched.ToString
    '            updateRef.PRReferenceFk = Nothing
    '        Else
    '            updateRef = r
    '            updateRef.PRLinkStatus = LinkStatus.Unmatched.ToString
    '        End If
    '        updateRef.PRProviderImportFk = ProvImport.IdAsInt

    '        NamesDataAccess.ReferenceData.InsertProviderReferenceRecord(trans, updateRef, SessionState.CurrentUser.Login)
    '    End If

    '    If row.Table.TableName = "tblProviderRIS" Then
    '        Dim r As New ProviderRIS(row, -1)

    '        'get existing record?
    '        Dim updateRis As ProviderRIS = ReferenceData.GetProviderRIS(row("PRISId").ToString)
    '        If updateRis IsNot Nothing Then
    '            updateRis.UpdatedFieldsFromProviderRIS(r)
    '            updateRis.PRISRISFk = -1
    '        Else
    '            updateRis = r
    '        End If

    '        NamesDataAccess.ReferenceData.InsertUpdateProviderRISRecord(trans, row("PRISId").ToString, updateRis, SessionState.CurrentUser.Login)
    '    End If

    '    If row.Table.TableName = "tblProviderOtherData" Then
    '        'delete existing record
    '        OtherData.DeleteProviderOtherData(trans, Provider.IdAsInt, row("POtherDataRecordId").ToString)
    '        'insert update
    '        OtherData.InsertUpdateProviderOtherData(trans, ProvImport.IdAsInt, row, SessionState.CurrentUser.Login)
    '    End If

    'End Sub

End Class
